package com.jilian.mydemo.modul.my.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.EvaluateDto;
import com.jilian.mydemo.common.dto.EvaluateVo;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.my.adapter.HydraulicRatingAdapter;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 水工平分
 */
public class HydraulicRatingActivity extends BaseActivity implements CustomItemClickListener {
    private TextView tvScore;
    private RecyclerView recyclerview;
    private HydraulicRatingAdapter adapter;
    private List<EvaluateVo> datas;
    private LinearLayoutManager linearLayoutManager;
    private OrderViewModel viewModel;
    private SmartRefreshLayout srNoData;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_hydraulicrating;
    }

    /**
     * 初始化UI
     */
    @Override
    public void initView() {
        setNormalTitle("水工评分", v -> finish());
        tvScore = (TextView) findViewById(R.id.tv_score);
        srNoData = (SmartRefreshLayout) findViewById(R.id.sr_no_data);
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        linearLayoutManager = new LinearLayoutManager(this);
        datas = new ArrayList<>();
        recyclerview.setLayoutManager(linearLayoutManager);
        adapter = new HydraulicRatingAdapter(this,datas,this);
        recyclerview.setAdapter(adapter);
        srNoData.setEnableLoadMore(false);

    }

    @Override
    public void initData() {
        showLoadingDialog();
        evaluate();
    }

    /**
     * 水工评分
     */
    private void evaluate() {

        viewModel.evaluate();
        viewModel.getEvaluateDtoliveData().observe(this, new Observer<BaseDto<EvaluateDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<EvaluateDto> evaluateDtoBaseDto) {
                hideLoadingDialog();
                if(evaluateDtoBaseDto.isSuccess()){
                    srNoData.finishLoadMore();
                    tvScore.setText(evaluateDtoBaseDto.getData().getCurrentScore());
                    if(EmptyUtils.isNotEmpty(evaluateDtoBaseDto.getData().getEvaluateVos())){
                        datas.clear();
                        datas.addAll(evaluateDtoBaseDto.getData().getEvaluateVos());
                        srNoData.setVisibility(View.GONE);
                        recyclerview.setVisibility(View.VISIBLE);
                        adapter.notifyDataSetChanged();
                    }
                    else{
                        srNoData.setVisibility(View.VISIBLE);
                        recyclerview.setVisibility(View.GONE);
                    }

                }
                else{
                    ToastUitl.showImageToastTips(evaluateDtoBaseDto.getMsg());
                    srNoData.setVisibility(View.VISIBLE);
                    recyclerview.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    public void initListener() {
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                evaluate();
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
